package lv.ante.dziesmas;

import java.math.BigDecimal;

public class BoundingBox {

	private float viewBoxWidth;
	private float viewBoxHeight;
	private float pieceBottom;
	private float markupBottom;
	private float pieceLeft;
	private float opusLeft;
	private int widthPx;
	private int heightPx;

	private BigDecimal x0;
	private BigDecimal y0;
	private BigDecimal width;
	private BigDecimal height;

	// in SVG units (NuArdievuVidzemite.ly)
	/*
	private final int deltaHeightPx = -11;
	private final float deltaY0 = 0.64F;
	private final float deltaHeight = -1.72F;
	private final float deltaX0 = -1.35F;
	private final float deltaWidth = 2.29F;
	*/
	private final int deltaHeightPx = -12;
	private final float deltaY0 = 0.64F*1.405838918F;
	private final float deltaHeight = -1.72F*1.405838918F;
	private final float deltaX0 = -1.35F*1.405838918F;
	private final float deltaWidth = 3.75F;
	

	public BoundingBox(String viewBox, String pieceBottom, String markupBottom,
			String pieceLeft, String opusLeft, int widthPx, int heightPx) {

		String[] fourParts = viewBox.split(" ");
		this.viewBoxWidth = Float.parseFloat(fourParts[2]);
		this.viewBoxHeight = Float.parseFloat(fourParts[3]);
		this.pieceBottom = Float.parseFloat(pieceBottom);
		this.markupBottom = Float.parseFloat(markupBottom);
		this.pieceLeft = Float.parseFloat(pieceLeft);
		this.opusLeft = Float.parseFloat(opusLeft);
		if (this.pieceLeft > this.opusLeft) {
			float temp = this.pieceLeft;
			this.pieceLeft = this.opusLeft;
			this.opusLeft = temp;
		}
		
		
		float unitsPerMmHorizontal = viewBoxWidth / 210.0F;
		float unitsPerMmVertical = viewBoxHeight / 297.0F;
		System.out.println("unitsPerMmHorizontal = " + unitsPerMmHorizontal);
		System.out.println("unitsPerMmVertical = " + unitsPerMmVertical);
		float unitsPerMm = (unitsPerMmHorizontal + unitsPerMmVertical)/2.0F;

		this.widthPx = widthPx;
		this.heightPx = heightPx;

		y0 = new BigDecimal(this.pieceBottom + deltaY0*unitsPerMm).setScale(2,
				BigDecimal.ROUND_HALF_UP);

		height = new BigDecimal(this.markupBottom - y0.floatValue()
				+ deltaHeight*unitsPerMm).setScale(2, BigDecimal.ROUND_HALF_UP);

		x0 = new BigDecimal(this.pieceLeft + deltaX0*unitsPerMm).setScale(2,
				BigDecimal.ROUND_HALF_UP);
		x0 = x0.max(BigDecimal.ZERO);

		width = new BigDecimal(this.opusLeft - x0.floatValue() + deltaWidth*unitsPerMm)
				.setScale(2, BigDecimal.ROUND_HALF_UP);
	}

	public String toString() {
		return "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.2\" width=\""
				+ widthPx
				+ "\" height=\""
				+ (heightPx + deltaHeightPx)
				+ "\" viewBox=\""
				+ x0.toPlainString()
				+ " "
				+ y0.toPlainString()
				+ " "
				+ width.toPlainString()
				+ " "
				+ height.toPlainString() + "\">";

	}

	public String toRectString() {
		return "<rect x=\""
				+ x0.toPlainString()
				+ "\" y=\""
				+ y0.toPlainString()
				+ "\" width=\""
				+ width.toPlainString()
				+ "\" height=\""
				+ height.toPlainString()
				+ "\" style=\"stroke:#ff0000; fill:white; stroke-width:0.2\" />";
	}

}
